package com.application.lucky.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.activity.me.RechargeCardActivity;
import com.application.lucky.activity.me.RechargeTypeActivity;
import com.application.lucky.bean.jsonBean.RedPListbean;
import com.application.lucky.manager.RedManager;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.widget.Keyboard;
import com.application.lucky.widget.PayEditText;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.qunar.im.ui.util.MoneyUtils;

public class InputPassworDialog extends BaseBottomEnterDialog {
    private static final String[] KEY = new String[]{
            "1", "2", "3",
            "4", "5", "6",
            "7", "8", "9",
            "完成", "0", "<<"
    };
    private ImageView mIvClose;
    private TextView mTvMoneyNum;
    private TextView mTvMoneyYue;
    private PayEditText mPayEditTextPay;
    private Keyboard mKeyboardViewPay;
    private SendSuccessListener listener;

    public InputPassworDialog(@NonNull Context context, SendSuccessListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected void initViews(Context context) {
        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mTvMoneyNum = (TextView) findViewById(R.id.tv_money_num);
        mTvMoneyYue = (TextView) findViewById(R.id.tv_money_yue);
        mPayEditTextPay = (PayEditText) findViewById(R.id.PayEditText_pay);
        mKeyboardViewPay = (Keyboard) findViewById(R.id.KeyboardView_pay);

        mKeyboardViewPay.setKeyboardKeys(KEY);
        initEvent();
    }

    @Override
    protected int layoutResId() {
        return R.layout.dialog_input_pay_password;
    }

    private void initEvent() {
        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mKeyboardViewPay.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position < 11 && position != 9) {
                    mPayEditTextPay.add(value);
                } else if (position == 9) {

                } else if (position == 11) {
                    mPayEditTextPay.remove();
                    //当点击完成的时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
//                    Toast.makeText(getApplication(), "您的密码是：" + payEditText.getText(), Toast.LENGTH_SHORT).show();
//                    finish();

                }
            }
        });

        /**
         * 当密码输入完成时的回调
         */
        mPayEditTextPay.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {
//                if (password.equals(UserManagers.getInstance().getPayPassword())) {
                sendRp(password);
//                } else {
//                    mPayEditTextPay.clearPw();
//                    ToastUtils.show("密码错误！");
//                }

            }
        });
    }

    private void sendRp(String password) {
        try {
            if (UserManagers.getInstance().getBalance()<Integer.valueOf(money)) {
                new TwoButtonDialog.Build(getContext())
                        .setMessage("余额不足，请充值！")
                        .disMiss(true)
                        .setLeftButton("取消", null)
                        .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                            @Override
                            public void click() {
                                getContext().startActivity(new Intent(getContext(), RechargeCardActivity.class));
                            }
                        }).create().show();
            }
            int i = Integer.parseInt(MoneyUtils.getServerMoney(Integer.valueOf(money)));
            new RedPRequest().red_send(id, i+"", rpNum, select, password, new CallBackEx<BaseResponse<RedPListbean>>() {
                @Override
                public void onFailed(int code) {
                    if (getContext() == null) {
                        return;
                    }
                    if (getContext() instanceof Activity) {
                        if (((Activity) getContext()).isFinishing() || ((Activity) getContext()).isDestroyed()) {
                            return;
                        }
                    }

                    if (code == 60005) {
                        new TwoButtonDialog.Build(getContext())
                                .setMessage("余额不足，请充值！")
                                .disMiss(true)
                                .setLeftButton("取消", null)
                                .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                                    @Override
                                    public void click() {
                                        getContext().startActivity(new Intent(getContext(), RechargeCardActivity.class));
                                    }
                                }).create().show();
                    } else if (code == 60006) {
                        mPayEditTextPay.clearPw();
                        ToastUtils.show("密码错误！");
                    }
                }

                @Override
                public void onSuccess(BaseResponse<RedPListbean> stringBaseResponse) {
                    UserManagers.getInstance().loadBalance();
                    if (getContext() == null) {
                        return;
                    }
                    if (listener != null) {
                        listener.success();
                    }
                    if (getContext() instanceof Activity) {
                        if (((Activity) getContext()).isFinishing() || ((Activity) getContext()).isDestroyed()) {
                            return;
                        }
                    }
                    RedManager.saveLieDian(select);

                    RedManager.saveMoney(money);
//                hideLoadDialog();
                    RedPListbean dataInfo = stringBaseResponse.dataInfo;
                    if (dataInfo != null) {
                        EventBusUtils.post(dataInfo);

                    }
                }
            });
        }catch (Exception e){

        }

    }

    private String id;
    private String money;
    private String rpNum;
    private String select;

    public void setData(String id, String money, String rpNum, String select) {
        this.id = id;
        this.money = money;
        this.rpNum = rpNum;
        this.select = select;
        mTvMoneyNum.setText("¥ " + money);
        mTvMoneyYue.setText("¥" + UserManagers.getInstance().getBalance());
        mPayEditTextPay.clearPw();
    }


    public interface SendSuccessListener {
        void success();
    }


}
